package ncochran.view;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Font;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.border.EmptyBorder;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;

import ncochran.controller.AddEdgeController;
import ncochran.controller.ChoiceController;
import ncochran.controller.StartButtonController;
import ncochran.model.DecisionLinesEvent;
import ncochran.model.Line;

import javax.swing.LayoutStyle.ComponentPlacement;

import com.sun.org.apache.xpath.internal.axes.SelfIteratorNoPredicate;
import javax.swing.JLabel;

/**
 * This is the main GUI for the DecisionLinesEvent.
 * It contains all of the text fields, start button, and a layout for drawing the Lines and Edges.
 * @author Nigel Cochran
 *
 */
public class DecisionLinesGUI extends JFrame {
	
	DecisionLinesEvent event;
	JPanel contentPane;
	LinesPanel linesPanel;
	JPanel choicesPanel;
	JButton startButton;
	JTextField[] choices;
	Dimension sizeChoice;
	Dimension sizeLines;
	JLabel textDisplay;
	JPanel panel;
	

	//The height and width of the manin window
	static final int TOTAL_HEIGHT = 550;
	static final int TOTAL_WIDTH = 850;
	
	//dimensions of the panels
	
	//the choices and lines panels share a width but have different heights
	static final int WIDTH = TOTAL_WIDTH - 150;
	static final int LINESHEIGHT = 350;
	//the height of the choice panel
	static final int CHOICESHEIGHT = 50;
	//the width of each individual choice text field
	static final int CWIDTH = 75;
	
	
	/**
	 * This method creates the actual Gui.
	 */
	public DecisionLinesGUI(DecisionLinesEvent event) {
		this.event = event;
		
		//takes the height and width and makes dimensions
		sizeChoice = new Dimension(WIDTH, CHOICESHEIGHT);
		sizeLines = new Dimension(WIDTH, LINESHEIGHT);
		
		
		//formatting stuff
		setResizable(false);
		toFront();
		setTitle("DecisionLines");
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, TOTAL_WIDTH, TOTAL_HEIGHT);
		
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		contentPane.setLayout(new BorderLayout(0, 0));
		setContentPane(contentPane);
		
		panel = new JPanel();
		panel.setToolTipText("");
		contentPane.add(panel, BorderLayout.CENTER);
		
		//creates the panel which shows the decision lines. need to comment this out to get window builder to work
		linesPanel = new LinesPanel(event, this);
		//need to comment this in to get window builder to work
		//JPanel linesPanel = new JPanel();
		
		//creates the panel with the choices
		choicesPanel = new JPanel();
		choicesPanel.setLayout(null);
		
		//the array of the text fields for the choices
		choices = new JTextField[event.getNumChoices()];
		
		//sets up each choice
		Dimension size = new Dimension(WIDTH, CHOICESHEIGHT);
		int spacing = size.width/ event.getNumChoices();
		int offset = spacing/2;
		for (int i = 0; i < event.getNumChoices(); i++) {
			choices[i] = new JTextField();
			choices[i].setEditable(true);
			choices[i].setColumns(1);
			choices[i].setBounds(offset + spacing*i - (CWIDTH/2), 10, CWIDTH, 25);
			choices[i].setHorizontalAlignment(JTextField.CENTER);
			//choices[i].addActionListener(new ChoiceController(event, event.getLine(i), choices[i]));
			choices[i].addActionListener(new ChoiceController(event, this, i));
			choicesPanel.add(choices[i]);
			
		}
		
		
		//Sets up the Panel containing the start button
		JPanel startPanel = new JPanel();
		startButton = new JButton();
		//startButton.setBounds(0, 0, 20, 20);
		startButton.setText("Start");
		startButton.addActionListener(new StartButtonController(event, this));
		startButton.setVisible(false);
		startPanel.add(startButton);
		
		//sets up the currently unused results panel.  just displays the results in the console right now
		JPanel resultsPanel = new JPanel();
		textDisplay = new JLabel();
		textDisplay.setText("Please enter your choices until all boxes are green.");
		resultsPanel.add(textDisplay);
		
		//more autogenerated formatting stuff from windowbuilder
		//just edit the constants at the top to resize stuff
		GroupLayout gl_panel = new GroupLayout(panel);
		gl_panel.setHorizontalGroup(
			gl_panel.createParallelGroup(Alignment.LEADING)
				.addGroup(Alignment.TRAILING, gl_panel.createSequentialGroup()
					.addGap(48)
					.addGroup(gl_panel.createParallelGroup(Alignment.TRAILING)
						.addComponent(resultsPanel, Alignment.LEADING, GroupLayout.DEFAULT_SIZE, WIDTH, Short.MAX_VALUE)
						.addComponent(linesPanel, Alignment.LEADING, GroupLayout.DEFAULT_SIZE, WIDTH, Short.MAX_VALUE)
						.addComponent(choicesPanel, GroupLayout.DEFAULT_SIZE, 691, Short.MAX_VALUE))
					.addGap(18)
					.addComponent(startPanel, GroupLayout.PREFERRED_SIZE, 67, GroupLayout.PREFERRED_SIZE)
					.addContainerGap())
		);
		gl_panel.setVerticalGroup(
			gl_panel.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_panel.createSequentialGroup()
					.addGap(22)
					.addGroup(gl_panel.createParallelGroup(Alignment.LEADING)
						.addComponent(startPanel, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
						.addGroup(gl_panel.createSequentialGroup()
							.addComponent(choicesPanel, GroupLayout.PREFERRED_SIZE, CHOICESHEIGHT, GroupLayout.PREFERRED_SIZE)
							.addPreferredGap(ComponentPlacement.UNRELATED)
							.addComponent(linesPanel, GroupLayout.PREFERRED_SIZE, LINESHEIGHT, GroupLayout.PREFERRED_SIZE)
							.addPreferredGap(ComponentPlacement.UNRELATED)
							.addComponent(resultsPanel, GroupLayout.PREFERRED_SIZE, CHOICESHEIGHT, GroupLayout.PREFERRED_SIZE)))
					.addContainerGap())
		);
		panel.setLayout(gl_panel);
	}
	
	/**
	 * This method returns the Dimension of the width and height of the Choices panel.
	 * @return Dimension The dimension the Choices panel
	 */
	public Dimension getSizeChoice() {
		return sizeChoice;
	}

	/**
	 * This method sets the Dimension of the width and height of the Choices panel.
	 * @param sizeChoice Dimension A Dimension which specifies the width and height of the Choices panel.
	 */
	public void setSizeChoice(Dimension sizeChoice) {
		this.sizeChoice = sizeChoice;
	}


	/**
	 * This method returns the Dimension of the width and height of the LinesPanel.
	 * @return Dimension The Dimension containing the width and height of the LinesPanel
	 */
	public Dimension getSizeLines() {
		return sizeLines;
	}

	/**
	 * This method sets the Dimension of the width and height of the LinesPanel.
	 * @param sizeLines Dimension The Dimension of the widht and height of the LinesPanel
	 */
	public void setSizeLines(Dimension sizeLines) {
		this.sizeLines = sizeLines;
	}
	
	/**
	 * This method sets all of the Choices text fields to not be editable.
	 */
	public void fixChoices(){
		for (int i = 0; i < choices.length; i++) {
			choices[i].setEditable(false);
		}
	}


	/**
	 * This method returns an array of text fields which correspond to the Choices.
	 * @return JTextField[] An array of text fields which correspond to the Choices
	 */
	public JTextField[] getChoices() {
		return choices;
	}
	
	/**
	 * This method returns the text field at the specified location.
	 * @param index int The desired location
	 * @return JTextField The choice text field at the desired location
	 */
	public JTextField getChoice(int index){
		return choices[index];
	}

	/**
	 * This method sets an array of text fields which correspond to the Choices.
	 * @param choices JTextField[] The desired array of text fields to correspond to the Choices
	 */
	public void setChoices(JTextField[] choices) {
		this.choices = choices;
	}

	/**
	 * This method returns the Start button.
	 * @return JButton The Start button
	 */
	public JButton getStartButton() {
		return startButton;
	}

	/**
	 * This method sets the Start button.
	 * @param startButton JButton The button to be the Start button
	 */
	public void setStartButton(JButton startButton) {
		this.startButton = startButton;
	}

	/**
	 * This method returns the LinesPanel which is used to draw Lines and Edges.
	 * @return LinesPanel The LinesPanel where Lines and Edges are drawn
	 */
	public LinesPanel getLinesPanel() {
		return linesPanel;
	}

	/**
	 * This method sets the LinesPanel which is used to draw Lines and Edges.
	 * @param linesPanel LinesPanel The LinesPanel where Lines and Edges are drawn
	 */
	public void setLinesPanel(LinesPanel linesPanel) {
		this.linesPanel = linesPanel;
	}

	/**
	 * This method returns the text label which provides the user with useful information.
	 * @return JLabel The text label.
	 */
	public JLabel getTextDisplay() {
		return textDisplay;
	}

	/**
	 * This method sets the text label which provides the user with useful information.
	 * @param textDisplay JLabel The label to be added.
	 */
	public void setTextDisplay(JLabel textDisplay) {
		this.textDisplay = textDisplay;
	}
	
	/**
	 * This method returns the main panel for the Gui.
	 * @return JPanel The main panel for the Gui.
	 */
	public JPanel getPanel(){
		return panel;
	}
	
	
}
